﻿using System;
using System.Net;
using System.Xml;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using DXTools.UI.Core;
using DXTools.UI.Modules.Configuration.Views;
using DXTools.UI.Core.Events;
using DXTools.UI.Core.ViewModel;
using DXTools.UI.Modules.Configuration.Events;
using DXTools.Core.Model;


namespace DXTools.UI.Modules.Configuration.ViewModels
{
    public class ToolbarViewModel : ViewModelBase
    {

        public ToolbarViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer container)
            : base(regionManager, eventAggregator, container)
        {
            this.HeaderInfo = "Configuration";
            this.ShowInstalledToolsViewCommand = new DelegateCommand<object>(this.ShowInstalledToolsView, this.CanShowInstalledToolsView);
            this.ShowCatalogViewCommand = new DelegateCommand<object>(this.ShowCatalogView, this.CanShowCatalogView);
            this.ShowSitemapViewCommand = new DelegateCommand<object>(this.ShowSitemapView, this.CanShowSitemapView);
            this.DonateCommand = new DelegateCommand<object>(this.ShowDonateView, this.CanShowDonateView);
        }

        #region Show Installed Tools

        public DelegateCommand<object> ShowInstalledToolsViewCommand { get; private set; }

        private void ShowInstalledToolsView(object commandArg)
        {
            // Single View
            string viewName = typeof(InstalledToolsView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                InstalledToolsView newView = UnityContainer.Resolve<InstalledToolsView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);
         
        }

        private bool CanShowInstalledToolsView(object commandArg)
        {
            return true;
        }

        #endregion

        #region Show Catalog

        public DelegateCommand<object> ShowCatalogViewCommand { get; private set; }

        private void ShowCatalogView(object commandArg)
        {
            // Single View
            string viewName = typeof(CatalogView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                CatalogView newView = UnityContainer.Resolve<CatalogView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);

        }

        private bool CanShowCatalogView(object commandArg)
        {
            return true;
        }

        #endregion

        #region Sitemap
        public DelegateCommand<object> ShowSitemapViewCommand { get; private set; }


        private void ShowSitemapView(object commandArg)
        {
            // Single View
            string viewName = typeof(SitemapView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                SitemapView newView = UnityContainer.Resolve<SitemapView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);

        }

        private bool CanShowDonateView(object commandArg)
        {
            return true;
        }

        #endregion

        #region Donate
        public DelegateCommand<object> DonateCommand { get; private set; }

       
        private void ShowDonateView(object commandArg)
        {
            // Single View
            string viewName = typeof(DonateView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                DonateView newView = UnityContainer.Resolve<DonateView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);

        }

        private bool CanShowSitemapView(object commandArg)
        {
            return true;
        }
       
        #endregion

        // Export to Excel test code
        //SaveFileDialog dlg = new SaveFileDialog();
        //dlg.Filter = "Excel Document (.xlsx)|*.xlsx|Zip Files (.zip)|*.zip";
        //dlg.DefaultExt = ".docx";
        //if (dlg.ShowDialog() == true)
        //{
        //    DXTools.Core.OXML.Excel.SpreadSheetDocument doc = new DXTools.Core.OXML.Excel.SpreadSheetDocument();
        //    doc.ApplicationName = "Test Excel";
        //    doc.Creator = "Rhett Clinton";
        //    doc.Company = "My Company";

        //    using (DXTools.Core.OXML.Packaging.IStreamStorage storage = new DXTools.Core.OXML.Packaging.ZipStreamStorage(dlg.OpenFile()))
        //    {
        //        doc.Save(storage);
        //    }
        //}
         
    }
}
